package com.bestcem.xm.award.dao.impl.mysql;

import com.bestcem.xm.award.dao.AwardRedPacketStatsDao;
import com.bestcem.xm.award.dao.mapper.AwardRedPacketStatsMapper;
import com.bestcem.xm.award.entity.mysql.AwardRedPacketStatsEntity;
import com.bestcem.xm.award.entity.pojo.RedPacketStatsCacheDO;
import com.bestcem.xm.common.core.uitls.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author xa.zhang <xa.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/10/21 11:31
 */
@Repository
public class AwardRedPacketStatsDaoImpl implements AwardRedPacketStatsDao {

    @Resource
    private AwardRedPacketStatsMapper redPacketStatsMapper;

    @Override
    public RedPacketStatsCacheDO selectByAwardId(String awardId) {
        AwardRedPacketStatsEntity entity = redPacketStatsMapper.selectByPrimaryKey(awardId);
        if (entity == null) {
            return null;
        }
        RedPacketStatsCacheDO awardPointStatsDO = new RedPacketStatsCacheDO();
        BeanUtils.copyProperties(entity, awardPointStatsDO);

        return awardPointStatsDO;
    }

    @Override
    public int insert(String awardId, long count, long amount) {
        AwardRedPacketStatsEntity entity = new AwardRedPacketStatsEntity();
        entity.setAwardId(awardId);
        entity.setAmount(amount);
        entity.setCount(count);
        entity.setCreateTime(DateUtil.getCommonDate());
        entity.setUpdateTime(DateUtil.getCommonDate());

        return redPacketStatsMapper.insert(entity);
    }

    @Override
    public int addCountAndAmountByAwardId(String awardId, long count, long amount) {
        AwardRedPacketStatsEntity entity = redPacketStatsMapper.selectByPrimaryKey(awardId);
        if (entity == null) {
            return this.insert(awardId, count, amount);
        } else {
            return redPacketStatsMapper.addCountAndAmountByAwardId(awardId, count, amount, DateUtil.getCommonDate());
        }

    }
}
